/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-10-11 20:50:01
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-10-26 21:01:55
 * @FilePath: /server/workspace/src/net/address.h
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once

#include <cstdint>
#include <istream>
#include <memory>
#include <netinet/in.h>
#include <ostream>
#include <sys/socket.h>
#include "sys/un.h"
namespace Yizhi {
    class Address{
        public: 
        typedef std::shared_ptr<Address> ptr;

        virtual ~Address()=default;

        int getFamily() const;

        virtual const sockaddr* getAddr() const=0;
        virtual socklen_t getAddrLen() const=0;

        virtual std::ostream& insert(std::ostream& os)const;

        std::string tostring();
         bool operator!=(const Address& rhs) const;
         bool operator<(const Address& rhs) const;
         bool operator==(const Address &rhs) const;
        
    };


    class IPAddress:public Address{
        public:
        typedef  std::shared_ptr<IPAddress> ptr ;

        


        virtual IPAddress::ptr networkAddress(uint32_t prefix_len)=0;
        virtual IPAddress::ptr subnetMask(uint32_t prefix_len)=0;
        virtual IPAddress::ptr broadcastAddress(uint32_t prefix_len)=0;

        virtual uint32_t getPort()const =0;
        virtual void setPort(uint32_t v)=0; 
    };



    class IPv4Address:public IPAddress{
        public:
         typedef  std::shared_ptr<IPv4Address> ptr ;
      
         IPv4Address(uint32_t address,uint32_t port);   
         socklen_t getAddrLen() const override;
         const sockaddr* getAddr() const override;
         std::ostream& insert(std::ostream& os) const override;

         IPAddress::ptr networkAddress(uint32_t prefix_len) override;
         IPAddress::ptr subnetMask(uint32_t prefix_len) override;
         IPAddress::ptr broadcastAddress(uint32_t prefix_len) override;
         
         uint32_t getPort() const override;
         void setPort(uint32_t v) override;

         private:
         sockaddr_in m_addr;
    };


    class IPv6Address:public IPAddress{

        public:
        typedef  std::shared_ptr<IPv6Address> ptr ;
        
         IPv6Address();    
         IPv6Address(const char* address,uint32_t port);

         socklen_t getAddrLen() const override;
         const sockaddr* getAddr() const override;
         std::ostream& insert(std::ostream& os) const override;

         IPAddress::ptr networkAddress(uint32_t prefix_len) override;
         IPAddress::ptr subnetMask(uint32_t prefix_len) override;
         IPAddress::ptr broadcastAddress(uint32_t prefix_len) override;
         
         uint32_t getPort() const override;
         void setPort(uint32_t v) override;

         private: 
         sockaddr_in6 m_addr;
    };

    class UnixAddress:public Address{
        public:
         typedef std::shared_ptr<UnixAddress> ptr ;
         UnixAddress();
         UnixAddress(const std::string& path);

         socklen_t getAddrLen() const override;
         const sockaddr* getAddr() const override;
         std::ostream& insert(std::ostream& os) const override;


         private:
         sockaddr_un m_addr;
         socklen_t m_length;
    };



    class UnkownAddress:public Address{
        public:
        typedef std::shared_ptr<UnkownAddress> ptr ;
        UnkownAddress(int family);
        socklen_t getAddrLen() const override;
        const sockaddr* getAddr() const override;
        std::ostream& insert(std::ostream& os) const override;


         private:
         sockaddr m_addr;
    };

}